package com.ctgu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.*;
import com.ctgu.mapper.RecruitMapper;
import com.ctgu.model.vo.RecruitVo;
import com.ctgu.service.ICompanyService;
import com.ctgu.service.IEmployeeService;
import com.ctgu.service.IPositionService;
import com.ctgu.service.IRecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper, Recruit> implements IRecruitService {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ICompanyService companyService;
    @Override
    public ResponseResult getList(int page, int pageSize, String name, String require) {
        //分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Recruit> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Recruit::getCompanyName,name);
        queryWrapper.like(StringUtils.isNotEmpty(require),Recruit::getJobRequirements,require);
//        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        if(loginUser.getUser().getRoleId().equals(0)){
//            queryWrapper.eq(Employee::getCompanyId,loginUser.getUser().getId());
//        }
        //添加排序条件
        queryWrapper.orderByAsc(Recruit::getId);
        //执行查询
        this.page(pageInfo,queryWrapper);
        Page<RecruitVo> voPage=new Page<>();
        BeanUtils.copyProperties(pageInfo,voPage,"records");
        List<Recruit> records=pageInfo.getRecords();
        //做处理，把eid转换为相应的hr名字;
        List<RecruitVo> list=records.stream().map((item)->{
            RecruitVo recruitVo=new RecruitVo();
            BeanUtils.copyProperties(item,recruitVo);
            recruitVo.setHrName(employeeService.getById(item.getEId()).getName());
            return recruitVo;
        }).collect(Collectors.toList());
        voPage.setRecords(list);
        return new ResponseResult<>(200,"分页成功",voPage);
    }

    @Override
    public ResponseResult add_recruit(Recruit recruit) {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取员工id
        Integer id = loginUser.getUser().getId();
        LambdaQueryWrapper<Recruit> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Recruit::getJobRequirements,recruit.getJobRequirements());
        queryWrapper.eq(Recruit::getEId,id);
        Recruit one = getOne(queryWrapper);
        if(Objects.nonNull(one)){
            return new ResponseResult<>(400,"该岗位需求已经存在请重新输入",id);
        }
        recruit.setEId(id);
        //设置公司名
        Employee employee=employeeService.getById(id);
        Company company=companyService.getById(employee.getCompanyId());
        recruit.setCompanyName(company.getCompanyName());
        recruit.setDate(LocalDateTime.now());
        save(recruit);
        return new ResponseResult<>(200,employee.getName()+"发布招聘信息成功");
    }

    @Override
    public ResponseResult getMyPage(int page, int pageSize) {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取hr的id
        //分页构造器
        Page pageInfo=new Page(page,pageSize);
        Integer id = loginUser.getUser().getId();
        LambdaQueryWrapper<Recruit> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Recruit::getEId,id);
//        List<Recruit> list = list(queryWrapper);
        //执行查询
        this.page(pageInfo,queryWrapper);
        return new ResponseResult<>(200,"分页成功",pageInfo);
    }

    @Override
    public ResponseResult delete(Integer[] ids) {
        LambdaQueryWrapper<Recruit> wrapper=new LambdaQueryWrapper();
        wrapper.in(Recruit::getId,ids);
        remove(wrapper);
        return new ResponseResult<>(200,"删除成功");
    }
}
